<template>
  <div class="search">
    <div class="title">
      <div class="left">
        <van-icon name="search" />
        <input
          v-myFocus
          type="text"
          placeholder="请输入小区或地址"
          v-model.trim="keywords"
          @input="inputSearch"
        />
      </div>
      <div class="cancel" @click="$router.back()">取消</div>
    </div>
    <div class="content">
      <p
        v-for="item in searchResult.body"
        :key="item.community"
        v-html="lightWords(item.communityName, keywords)"
        @click="sendInfoToAdd(item)"
      ></p>
      <div
        class="empty"
        v-show="searchResult.status === 200 && searchResult.body.length === 0"
      >
        <h5>暂无小区哦...</h5>
      </div>
    </div>
  </div>
</template>

<script>
import { areaSearchKeywordAPI } from '@/api'
export default {
  name: 'Search',
  data() {
    return {
      keywords: '',
      // 防抖
      timer: null,
      // 搜寻结果
      searchResult: {}
    }
  },
  methods: {
    // 输入框事件
    /* eslint-disable */
    inputSearch() {
      clearTimeout(this.timer);
      this.timer = setTimeout(async () => {
        if (this.keywords.length === 0) return;
        const res = await areaSearchKeywordAPI({
          name: this.keywords,
          id: localStorage.getItem("code"),
        });
        // console.log(res)
        this.searchResult = res.data;
      }, 500);
      /* eslint-enable */
    },
    // 文字高亮事件
    lightWords(oldValue, target) {
      const resStr = new RegExp(target, 'ig')
      return oldValue.replace(
        resStr,
        (match) => `<span style="color: skyblue">${match}</span>`
      )
    },
    // 按下确认发送
    sendInfoToAdd(item) {
      this.$router.replace({
        name: 'add',
        params: item
      })
    }
  }
}
</script>

<style lang="less" scoped>
.search {
  .title {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 45px;
    background-color: #efeff4;
    display: flex;
    align-items: center;
    padding: 0 10px;
    .left {
      flex: 1;
      display: flex;
      align-items: center;
      background-color: #fff;
      height: 28px;
      padding: 0 30px 0 15px;
      .van-icon {
        font-size: 20px;
      }
      input {
        height: 20px;
        width: 100%;
        border: 0;
        margin-left: 5px;
        vertical-align: middle;
        font-size: 15px;
        color: burlywood;
        &::placeholder {
          color: rgb(136, 255, 165);
        }
        &:focus {
          color: rgb(255, 166, 213);
        }
      }
    }
    .cancel {
      width: 30px;
      font-size: 15px;
      margin-left: 10px;
      color: #6b7072;
    }
  }
  .content {
    padding: 50px 20px;
    p {
      height: 50px;
      border-bottom: 1px solid rgb(228, 228, 228);
      line-height: 50px;
      font-size: 14px;
      color: #333;
    }
    p:nth-last-child(1) {
      border-bottom: none;
    }
    .empty {
      margin-top: 50px;
      text-align: center;
      h5 {
        font-size: 15px;
        color: #333;
      }
    }
  }
}
</style>
